﻿using EventMgrLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionRoc.Core;
using VisionRoc.Tool;

namespace VisionRoc_Measure
{
    public partial class FormDisplayDataTool : Form
    {

        // 静态实例字段
        private static FormDisplayDataTool _instance;
        public FormDisplayDataTool()
        {
            InitializeComponent();
          
            EventMgr.Ins.GetEvent<SystemEvent>().Subscribe(ResultInfo, dat => dat.SolutionName == "DispData");
            Tool.IniDataGridView(dataGridView1);
        }

        private void FormDisplayDataTool_Load(object sender, EventArgs e)
        {
            //示例
            //EventMgr.Ins.GetEvent<SystemEvent>().Subscribe(SystemInfo1, dat => dat.SolutionName == "Image");
            //EventMgr.Ins.GetEvent<SystemEvent>().Subscribe(SystemInfo2, dat => dat.SolutionName == "winwon");
            //EventMgr.Ins.GetEvent<ResultNum>().Subscribe(ResultInfo, num => num.SolutionName == "Main");
        }

        // 全局访问点
        public static FormDisplayDataTool Instance
        {
            get
            {
                if (_instance == null || _instance.IsDisposed)
                {
                    _instance = new FormDisplayDataTool();
                }
                return _instance;
            }
        }

        #region 事件聚合器方法

        object dat = new object();
        long SN = 1;
        private void ResultInfo(Systemload number)
        {
            try
            {
                if(number.SolutionName== "DispData")
                {
                    lock (dat)
                    {
                        this.Invoke(new MethodInvoker(() =>
                        {
                            this.dataGridView1.Rows.Add(SN, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Global.PE_Dis, Global.NE_Dis, Global.DpeNe_Dis, Global.Camera5State, "");
                            dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.Count - 1;

                            if (Global.Camera5State)
                            {
                                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[5].Style.ForeColor = Color.Green;
                            }
                            else
                            {
                                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[5].Style.ForeColor = Color.Red;
                            }

                            if (dataGridView1.Rows.Count > Global.m_ImageSavePath.DataNum)
                            {
                                dataGridView1.Rows.RemoveAt(0);
                            }
                            dataGridView1.CurrentCell = null;
                            dataGridView1.Refresh();
                        }));
                        CsvAPI.WriteDataCsv(Global.m_ImageSavePath.DataPath, Global.PE_Dis, Global.NE_Dis, Global.DpeNe_Dis, Global.Camera5State);
                        SN++;
                    }
                }
              
            }
            catch (Exception ex)
            {
                TxtAPI.WriteLogTxt(Global.m_ImageSavePath.LogPath, ex.ToString());
            }
        }

        private void SystemInfo1(Systemload systemload)
        {

        }

        private void SystemInfo2(Systemload systemload)
        {

        }

        #endregion

        private void Tool_DataGridViewToCSV_Click(object sender, EventArgs e)
        {
            try
            {
                Tool.DataGridViewToCSV(dataGridView1);
            }
            catch (Exception ex)
            {
                MessageBox.Show("导出失败！" + ex.ToString());
            }
        }

        private void Tool_DataClear_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
        }
    }
}
